// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Springboard Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 39 : TYPE_SPRINGBOARD

// Function declarations
reserve function Springboard_DebugDraw
reserve function Springboard_DebugSpawn

// Static Values
public value Springboard_value32 = 0;

// Tables
public table Springboard_table34
	-0x40000, -0xA0000, -0x80000
end table

public table Springboard_table35
	0, 0, 0, 0, 0, 0, 0, 0, -0x10000, -0x10000, -0x10000, -0x10000, -0x10000, -0x10000, -0x10000, -0x10000
	-0x10000, -0x10000, -0x10000, -0x10000, -0x10000, -0x10000, -0x10000, -0x10000, -0x20000, -0x20000, -0x20000, -0x20000, -0x20000, -0x20000, -0x20000, -0x20000
	-0x30000, -0x30000, -0x30000, -0x30000, -0x30000, -0x30000, -0x40000, -0x40000
end table

public table Springboard_table36
	8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 20, 21
	21, 22, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24
end table

public table Springboard_table37
	8, 9, 10, 11, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 14, 14
	15, 15, 16, 16, 16, 16, 15, 15, 14, 14, 13, 13
end table


function Springboard_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


function Springboard_DebugSpawn
	temp0 = object[0].value17
	temp0 -= Springboard_value32
	CreateTempObject(TypeName[Springboard], 0, object.xpos, object.ypos)
	GetTableValue(object[tempObjectPos].value1, temp0, Springboard_table34)
	object[tempObjectPos].direction = object.direction
end function


event ObjectMain
	if object.value0 != 0
		object.value0--
		switch object.value0
		case 0
			temp2 = 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				GetBit(temp3, object.value2, temp2)
				if temp3 == 1
					temp0 = object[currentPlayer].xpos
					temp0 -= object.xpos
					temp0 += 0x1C0000
					temp0 >>= 17
					GetBit(temp3, object.direction, 0)
					if temp3 == 1
						temp0 -= 28
						FlipSign(temp0)
					end if
					if temp0 >= 8
						object[currentPlayer].value10 = ANI_WALKING
						if object[currentPlayer].animation == ANI_RUNNING
							object[currentPlayer].value10 = ANI_RUNNING
						end if
						if object[currentPlayer].animation == ANI_PEELOUT
							object[currentPlayer].value10 = ANI_PEELOUT
						end if
						object.value0 = 1
						object[currentPlayer].state = PlayerObject_HandleAir
						object[currentPlayer].tileCollisions = 1
						object[currentPlayer].gravity = GRAVITY_AIR
						object[currentPlayer].speed = object[currentPlayer].xvel
						temp0 -= 8
						temp0 <<= 1
						if temp0 > 39
							temp0 = 39
						end if
						GetTableValue(object[currentPlayer].yvel, temp0, Springboard_table35)
						object[currentPlayer].yvel -= 0x40000
						object[currentPlayer].yvel -= object[currentPlayer].value25
						object[currentPlayer].animation = ANI_TWIRL
						object[currentPlayer].prevAnimation = ANI_TWIRL
						object[currentPlayer].frame = 0
						object[currentPlayer].animationSpeed = 80
						object[currentPlayer].value1 = 0
						PlaySfx(SfxName[Spring], 0)
					end if
				end if
				temp2++
			next
			break
		case 1
			object.frame = 0
			break
		default
			object.frame = 1
			break
		end switch
	end if
	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp0 = object[currentPlayer].xpos
		temp0 -= object.xpos
		temp0 += 0x1C0000
		temp0 >>= 17
		if temp0 < 0
			temp0 = 0
		end if
		if temp0 > 27
			temp0 = 27
		end if
		switch object.direction
		case 0
			if object.frame == 0
				GetTableValue(temp1, temp0, Springboard_table36)
			else
				GetTableValue(temp1, temp0, Springboard_table37)
			end if
			FlipSign(temp1)
			temp4 = object[currentPlayer].speed
			temp5 = object[currentPlayer].xpos
			BoxCollisionTest(C_BOX, object.entityPos, -28, temp1, 28, -8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 2
				object[currentPlayer].speed = temp4
				object[currentPlayer].xpos = temp5
			case 0
			case 3
			case 4
				GetBit(temp3, object.value2, temp2)
				if temp3 == 1
					temp3 = object[currentPlayer].xpos
					temp3 -= object.xpos
					temp3 >>= 16
					Abs(temp3)
					if temp3 < 28
						if object[currentPlayer].yvel >= 0
							object[currentPlayer].iypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							temp1 <<= 16
							object[currentPlayer].ypos += temp1
							object[currentPlayer].ypos += object.ypos
							checkResult = 1
						end if
					end if
				end if
			end switch
			break
		case 1
			temp0 -= 27
			Abs(temp0)
			if object.frame == 0
				GetTableValue(temp1, temp0, Springboard_table36)
			else
				GetTableValue(temp1, temp0, Springboard_table37)
			end if
			FlipSign(temp1)
			temp4 = object[currentPlayer].speed
			temp5 = object[currentPlayer].xpos
			BoxCollisionTest(C_BOX, object.entityPos, -28, temp1, 28, -8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 3
				object[currentPlayer].speed = temp4
				object[currentPlayer].xpos = temp5
			case 0
			case 2
			case 4
				GetBit(temp3, object.value2, temp2)
				if temp3 == 1
					temp3 = object[currentPlayer].xpos
					temp3 -= object.xpos
					temp3 >>= 16
					Abs(temp3)
					if temp3 < 28
						if object[currentPlayer].yvel >= 0
							object[currentPlayer].iypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							temp1 <<= 16
							object[currentPlayer].ypos += temp1
							object[currentPlayer].ypos += object.ypos
							checkResult = 1
						end if
					end if
				end if
			end switch
			break
		case 2
			break
		case 3
			break
		end switch
		if checkResult == 1
			SetBit(object.value2, temp2, 1)
			if temp0 >= 8
				if object.value0 == 0
					object.value0 = 6
				end if
			end if
		else
			SetBit(object.value2, temp2, 0)
		end if
		temp2++
	next
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")
	foreach (TypeName[Springboard], arrayPos0, ALL_ENTITIES)
		GetTableValue(object[arrayPos0].value1, temp0, Springboard_table34)
	next
	SpriteFrame(-28, -24, 56, 16, 52, 160)
	SpriteFrame(-28, -16, 56, 8, 52, 177)
	temp0 = 0
	Springboard_value32 = DebugMode_ObjCount
	while temp0 < 3
		SetTableValue(TypeName[Springboard], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Springboard_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Springboard_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
